/**
 * @author karlwang
 * @Time：2018年7月20日 下午2:51:13
 * @version 1.0  
 */
package com.len.core.shiro;
/**
 * @author karlwang
 * @Time：2018年7月20日 下午2:51:13
 * @version 1.0
 */

 
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.len.core.matcher.JwtMatcher;
import com.len.core.matcher.PasswordMatcher;
import com.len.core.token.PasswordToken;
import com.len.core.token.JwtToken;
import com.len.service.SysUserService;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/* *
 * @Author tomsun28
 * @Description realm管理器
 * @Date 17:52 2018/3/3
 */
@Component
public class RealmManager {

    private PasswordMatcher passwordMatcher;
    private JwtMatcher jwtMatcher;
    private SysUserService sysUserService;
    @Autowired
    public RealmManager(SysUserService sysUserService,PasswordMatcher passwordMatcher,JwtMatcher jwtMatcher) {
        this.sysUserService = sysUserService;
        this.passwordMatcher = passwordMatcher;
        this.jwtMatcher = jwtMatcher;
    }

    public List<Realm> initGetRealm() {
        List<Realm> realmList = new LinkedList<>();
        // ----- password
        LoginRealm loginRealm = new LoginRealm();
        loginRealm.setUserService(sysUserService);
        loginRealm.setCredentialsMatcher(passwordMatcher);
        loginRealm.setAuthenticationTokenClass(PasswordToken.class);
        realmList.add(loginRealm);
        // ----- jwt
        JwtRealm jwtRealm = new JwtRealm();
        jwtRealm.setCredentialsMatcher(jwtMatcher);
        jwtRealm.setAuthenticationTokenClass(JwtToken.class);
        realmList.add(jwtRealm);
        return Collections.unmodifiableList(realmList);
    }
}
